<?php
class Application_Model_DbTable_Event extends Zend_Db_Table_Abstract
{
    protected $_name = 'event';
	protected $_referenceMap = array(
		'Tournament' => array(
			'columns' => 'tourn_id',
			'refTableClass' => 'Application_Model_DbTable_Tournament',
			'refColumns' => array('id')
		)
	);
    public function GetAllEvents($visible = true){
    	$where = null;
    	if($visible){
    		$where = 'visible = 1';
    	}
    	$res = $this->fetchAll($where);
    	return $res->toArray();
    }
    public function GetEvents($t_id, $visible = true){
    	$_t_id = (int)$t_id;
    	$select = $this->select();
    	if($visible){
    		$select->where('visible = ?', 1);
    	}
    	if($_t_id){
    		$select->where('tourn_id = ?', $_t_id);
    	}
    	$res = $this->fetchAll($select);
    	return $res->toArray();
    }
    public function GetEventById($id, $visible = true){
        $_id = (int)$id;
    	$select = $this->select();
    	if($visible){
    		$select->where('visible = ?', 1);
    	}
    	$select->where('id = ?', $_id);
    	$res = $this->fetchRow($select);
    	if(!$res){
    		throw new Zend_Db_Table_Exception('Cant find id ' . $_id, null);
    	}
    	return $res;
    }
    public function CheckDate($id){
    	$_id = (int)$id;
    	$select = $this->select();
    	$select->from('event', 'date_close');
    	$select->where('id = ?', $_id);
    	$res = $this->fetchRow($select);
    	$date_now = new Zend_Date();
    	$date = new Zend_Date($res['date_close']);
    	if($date->compare($date_now) == -1){
    		return false;
    	}
    	else{
    		return true;
    	}
    }
}
